package it.tcm.TeacherClassMate.action;

import java.util.List;

import it.tcm.TeacherClassMate.beans.DisequationBean;
import it.tcm.TeacherClassMate.beans.EquationBean;
import it.tcm.TeacherClassMate.facade.DisequationManageBase;
import it.tcm.TeacherClassMate.facade.EquationManageBase;
import it.tcm.TeacherClassMate.form.GeneralForm;
import it.tcm.TeacherClassMate.util.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class ChooseTypologyForTemplateAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods


	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		 logger.info("CHOOSE TYPOLOGY FOR TEMPLATE");
		 
		 GeneralForm mcaForm = (GeneralForm)form;	
		 Integer idtypology = null;
		 String mode = null;		 
		 if (mcaForm.getIdtypology() != null) { idtypology = (Integer)mcaForm.getIdtypology(); }
		 if (mcaForm.getMode() != null) { mode = (String)mcaForm.getMode(); }	
		 logger.info("idtypology " + idtypology);
		 logger.info("mode " + mode);

		 request.setAttribute(Util.FORM_GENERAL,mcaForm); 
		 
		 if (idtypology.equals(Util.EQUATIONS_TYPOLOGY)) {
			 // CHANGE IN EQUATION TYPOLOGY
			 EquationManageBase emb = new EquationManageBase();
			 List<EquationBean> equationBeanList = emb.getAll();
			 request.setAttribute("resultList", equationBeanList);
			 mcaForm =  new GeneralForm();
			 mcaForm.setIdtypology(Util.EQUATIONS_TYPOLOGY);
			 request.setAttribute(Util.FORM_GENERAL,mcaForm);
		 } else  if (idtypology.equals(Util.DISEQUATIONS_TYPOLOGY)) {
			 // CHANGE IN DISEQUATION TYPOLOGY
			 DisequationManageBase dmb = new DisequationManageBase();
			 List<DisequationBean> disequationBeanList = dmb.getAll();
			 request.setAttribute("resultList", disequationBeanList);
			 mcaForm =  new GeneralForm();
			 mcaForm.setIdtypology(Util.DISEQUATIONS_TYPOLOGY);
			 request.setAttribute(Util.FORM_GENERAL,mcaForm);
		 }
		 return mapping.findForward("showdatatemplate");
			 	 
	}
	
	
}